package liangtWorkSpace.httpClient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName HttpClientTest
 * @Description : 发送httpClient
 * @Author : LiangT
 * @Date : 2020/4/9 19:01
 */
public class HttpClientTest {

    /**
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public String post(String url,Map params) throws Exception{
        int timeOutTime = 600000;// 超时时间60S
        String paramsStr = JSONObject.toJSONString(params);
        return post(url,timeOutTime,paramsStr);
    }

    /**
     *
     * @param url
     * @param timeOutTime
     * @param params
     * @return
     * @throws Exception
     */
    public String post(String url,int timeOutTime,String params)throws Exception{
        String content = null;
        int TimeOutTime = timeOutTime;
        long time = System.currentTimeMillis();
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpContext httpContext = new BasicHttpContext();
        try {
            HttpPost post = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TimeOutTime)
                    .setConnectTimeout(TimeOutTime).build();// 设置请求和传输超时时间
            post.setHeader(new BasicHeader("Content-Type", "application/json;charset=UTF-8"));
            //设置期望服务端返回的编码

            HttpEntity entity = new StringEntity(JSON.toJSONString(params),"utf-8");
            post.setEntity(entity);
            post.setConfig(requestConfig);
            System.out.printf("执行post请求..." + post.getURI());
            // 执行请求
            httpResponse = httpClient.execute(post, httpContext);
            HttpEntity entity2 = httpResponse.getEntity();
            if (null != entity) {
                content = String.valueOf(EntityUtils.toString(entity2));
                System.out.printf(content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpResponse.close();
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.printf("http请求时间：" + (System.currentTimeMillis() - time) / 1000d + "s");
        }
        return content;
    }
    @Test
    public void test() {
        String url = "http://localhost:9100//scgdSupport/queryBill";
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("input1", "11000394");

        String response = null;
        try {
            response = post(url,params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("响应信息："+response);
    }
}
